package com.caseeye.util

import org.springframework.stereotype.Component

import org.springframework.beans.factory._
import org.springframework.beans.factory.config.AutowireCapableBeanFactory
import org.springframework.beans.factory.config.AutowireCapableBeanFactory._
import scala.reflect.BeanProperty
import com.caseeye.base.Entity

@Component
class RichDomainObjectFactory extends BeanFactoryAware {

    var factory : AutowireCapableBeanFactory = null

    def autowire[T <% Entity](entity : T) : T = {
        factory.autowireBeanProperties(entity, AUTOWIRE_NO, false);
        entity
    }

    def setBeanFactory(beanFactory : BeanFactory) : Unit = this.factory = beanFactory.asInstanceOf[AutowireCapableBeanFactory]
}

object RichDomainObjectFactory {
    val richDomainObjectFactory : RichDomainObjectFactory = new RichDomainObjectFactory
    
    def autowireFactory : RichDomainObjectFactory = richDomainObjectFactory

    def autowire[T <% Entity](entity : T) : T = richDomainObjectFactory.autowire(entity)

}

